package libase.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author aspr0
 */
@Entity
@Table (name = "PUBLISHER")
@XmlRootElement
@NamedQueries (
        {
    @NamedQuery (name = "Publisher.findAll", query = "SELECT p FROM Publisher p order by p.id"),
    @NamedQuery (name = "Publisher.findById", query = "SELECT p FROM Publisher p WHERE p.id = :id"),
    @NamedQuery (name = "Publisher.findByName", query = "SELECT p FROM Publisher p WHERE p.name = :name")
})
public class Publisher implements Serializable
{
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @SequenceGenerator (allocationSize = 1, name = "spg", sequenceName = "SEQ_PUBLISHER")
    @GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "spg")
    @Id
    @Basic (optional = false)
    @NotNull
    @Column (name = "ID")
    private BigDecimal id;
    @Basic (optional = false)
    @NotNull
    @Size (min = 1, max = 200)
    @Column (name = "NAME")
    private String name;
    @OneToMany (cascade = CascadeType.ALL, mappedBy = "pub")
    private Collection<Book> bookCollection;

    public Publisher()
    {
    }

    public Publisher(BigDecimal id)
    {
        this.id = id;
    }

    public Publisher(BigDecimal id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public BigDecimal getId()
    {
        return id;
    }

    public void setId(BigDecimal id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @XmlTransient
    public Collection<Book> getBookCollection()
    {
        return bookCollection;
    }

    public void setBookCollection(
            Collection<Book> bookCollection)
    {
        this.bookCollection = bookCollection;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Publisher))
        {
            return false;
        }
        Publisher other = (Publisher) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return name;
    }
}
